/**
 * Created by zhu on 2016/2/6.
 */
var indexId;
$(function(){
    initWheel();
    initBest();
    initHot();
    $('#hotSaveBtn').on('click',function(){
        $('#hotAdd').modal('hide');
        var myid = $('#addId').val();
        var myheat = $('#addheat').val();
        $.post(contextPath+'/mmt_admin/index/save',{heat:myheat,goodsId:myid},function(data){
            if(data.status == 0){
                initHot();
            }else{
                alert("修改失败");
            }
        },"json");
    });

    $('#hotChange').on('click',function(){
        $('#hotEdit').modal('hide');
        var myid = $('#hotId').val();
        var myheat = $('#heat').val();
        $.post(contextPath+'/mmt_admin/index/'+indexId,{heat:myheat,goodsId:myid},function(data){
            if(data.status == 0){
                initHot();
            }else{
                alert("修改失败");
            }
        },"json");
    });

    $('#changeSave').on('click',function(){

        $.get(contextPath+"/mmt_admin/indexs/"+indexId+"/"+$('#goodsId').val(),function(data){
            changeGood(data);
        })
    })

});

function changeGood(data){
    data = $.parseJSON(data);
    $('#myModal').modal('hide');
    if(data.status == 0){
        initWheel();
        initBest();
        alert("修改成功");
    }else{
        alert("修改失败");
    }

}

function initWheel(){
    $.get(contextPath+"/index/wheel",function(data){
        data = $.parseJSON(data);
        var wheel = $('#wheel');
        wheel.empty();
        $.each(data.list,function(i,item){
            var temp = '    <div class="col-md-4">' +
                '<img src="'+contextPath+item.goods.index_url+'" style="height: 140px;width: 140px" class="img-thumbnail">' +
                '<p style="margin-top: 10px;">商品名：'+item.goods.name+'</p>' +
                '<p  style="margin-left:43px;"><input type="hidden" value="'+item.id+'"><button id="wheelBtn" type="button" class="btn btn-success btn-sm wheelBtn" data-toggle="modal" data-target="#myModal">' +
                '修改' +
                '</button></p></div>' ;
            wheel.append(temp)
        });

        $('.wheelBtn').on('click',function(){
            indexId = $($(this).parent().find("input")[0]).val();
        })

    });
}

function initBest(){
    $.get(contextPath+"/index/best",function(data){
        data = $.parseJSON(data);
        var best = $('#best');
        best.empty();
        $.each(data.list,function(i,item){
           var temp = '    <div class="col-md-4">' +
               '<img src="'+contextPath+item.goods.index_url+'" style="height: 140px;width: 140px" class="img-thumbnail">' +
               '<p style="margin-top: 10px;">商品名：'+item.goods.name+'' +
               '<p style="margin-left:43px;"><input type="hidden" value="'+contextPath+item.id+'"><button type="button" class="btn btn-success btn-sm bestBtn" data-toggle="modal" data-target="#myModal">' +
               '修改' +
            '</button></p></div>' ;
            best.append(temp)
        });
        $('.bestBtn').on('click',function(){
            indexId = $($(this).parent().find("input")[0]).val();
        })
    });
}

function initHot(){
    $.get(contextPath+"/index/hot",function(data){
        data = $.parseJSON(data);
        var hot = $('#hot');
        hot.empty();
        $.each(data.list,function(i,item){
            var temp = '    <div class="col-md-4">' +
                '<img src="'+contextPath+item.goods.index_url+'" style="height: 140px;width: 140px" class="img-thumbnail">' +
                '<p style="margin-top: 10px;">商品名：'+item.goods.name+'</p><p>&nbsp热度：' +item.heat+
                '</p><p style="margin-left:20px;"><input type="hidden" value="'+item.goods.id+'">' +
                '<input type="hidden" value="'+item.heat+'">' +
                '<input type="hidden" value="'+item.id+'">' +
                '<button type="button" class="btn btn-success btn-sm hotEidtBtn" data-toggle="modal" data-target="#hotEdit">' +
                '修改' +'</button>'+
                '<button type="button" class="btn btn-primary btn-sm hotDelBtn">' +
                '删除' +'</button>'+
                '</p></div>' ;
            hot.append(temp)
        });
        $('.hotEidtBtn').on('click',function(){
            $('#hotId').val($($(this).parent().find("input")[0]).val());
            $('#heat').val($($(this).parent().find("input")[1]).val());
            indexId = $($(this).parent().find("input")[2]).val();
        });
        $('.hotDelBtn').on('click',function(){
            indexId = $($(this).parent().find("input")[0]).val();
            $.ajax({
                url: contextPath+'/mmt_admin/index/'+indexId,
                type: 'DELETE',
                success: function(result) {
                    initHot();
                }
            });

        });

    });
}